package thread.thread014;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-25 19:51
 **/
public class Foodie implements Runnable {
    private final Desk desk;

    public Foodie(Desk desk) {
        this.desk = desk;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (desk.getLock()) {
                int count = desk.getCount();
                if (count <= 0) {
                    break;
                } else {
                    if (desk.isFlag()) {
                        desk.setCount(--count);
                        System.out.println(Thread.currentThread().getName() + " 消费包子, 剩余 " + count);
                        desk.setFlag(false);
                        desk.getLock().notifyAll();
                    } else {
                        try {
                            desk.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }

                }
            }
        }
    }
}
